<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到小邓甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              type="primary"
              size="default"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
// 引入userStore
import useUserStore from '../../store/modules/user'
// 引入路由
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
// 引入获取当前时间方法
import { getTime } from '../../util/time'

//收集账号与密码数据
let loginForm = reactive({ username: 'admin', password: '111111' })
// userStore
let userStore = useUserStore()
// 登录按钮状态
let loading = ref(false)
let loginForms = ref()
// 路由器
let $router = useRouter()

// 路由
let $route = useRoute()

// 判断登陆的时候是否有query参数，如果有就往query参数携带的路由跳转，如果没有就跳转到首页
let redirect: any = $route.query.redirect

//自定义校验规则函数
const validatorUserName = (rule: any, value: any, callback: any) => {
  //rule：校验规则对象
  //value:表单元素文本内容
  //callback:符合条件，callback放行通过，不符合：注入错误提示信息
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('账号长度至少5位'))
  }
}

const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码长度至少6位'))
  }
}

// 定义表单校验配置对象
const rules = {
  username: [{ trigger: 'change', validator: validatorUserName }],
  password: [{ trigger: 'change', validator: validatorPassword }],
}

// 登录方法
const login = async () => {
  // 保证所有表单项校验通过再发送请求
  await loginForms.value.validate()

  // 一点击登录按钮 就设置loading状态为true
  loading.value = true
  try {
    // 设置发起登录请求是londing状态
    // 使用pinia来进行发送登录请求
    // 使用异步 当登录成功之后 返回成功的pormis 并且通过路由跳转到首页
    // 登录失败 提示失败信息
    await userStore.userLogin(loginForm)
    // 编程式导航跳转到展示数据的首页
    $router.push({ path: redirect || '/' })
    // 登录成功提示信息
    ElNotification({
      type: 'success',
      message: '登录成功',
      title: `Hi,${getTime()}好`,
    })
    // 登录成功后设置loading状态为false
    loading.value = false
  } catch (error) {
    // 登录失败 加载效果也消失
    loading.value = false
    // 登录失败提示信息
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  }
}
</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
      background: cornflowerblue;
    }
  }
}
</style>
